class Solution {
    constructor(head) {
        this.head = head;
    }

    getRandom() {
        let res = this.head.val;
        let cur = this.head;
        let i = 1;
        while (cur.next) {
            cur = cur.next;
            i++;
            if (Math.floor(Math.random() * i) === 0) {
                res = cur.val;
            }
        }
        return res;
    }
}